package com.tom;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;




import java.io.InputStreamReader;
import java.io.Reader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 * Servlet implementation class RegServlet
 */
public class RegServlet extends HttpServlet {
	/*字型轉換的method
	private String b2u(String str2convert) throws IOException {
	      StringBuffer buffer = new StringBuffer();
	      byte[] targetBytes = str2convert.getBytes();

	      ByteArrayInputStream stream = new ByteArrayInputStream(targetBytes);
	      InputStreamReader isr=new InputStreamReader(stream,"iso-8859-1");
	      Reader in = new BufferedReader(isr);
	      int chInt;
	      while ( (chInt = in.read()) > -1 ) {
	         buffer.append((char)chInt);
	      }
	      in.close();
	      return buffer.toString();
	   }*/
	
        private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        	doPost(request, response);   
        	/*String userid = null;
                String name = null;
                String nickname = null;
                String pw1 = null;
                String pw2 = null;
                String PID = null;
                String sex = null;
                String phone = null;
                String logintype = null;
                String email = null;
                
                request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8");                
                
                if (request.getParameter("userid")!=null){
                        userid = request.getParameter("userid");
                        nickname = request.getParameter("nickname");
                        pw1 = request.getParameter("pw1");
                        pw2 = request.getParameter("pw2");
                        PID = request.getParameter("PID");
                        sex = request.getParameter("sex");
                        phone = request.getParameter("phone");
                        email = request.getParameter("email");
                        logintype = request.getParameter("logintype");
                                                                      
                        Member m = new Member(userid, name, nickname, pw1, pw2, PID, sex, phone, email, logintype);
                        HttpSession session = request.getSession();
                        session.setAttribute("m", m);
                        
                        if (m.validate()){
                                // 註冊成功 , Forward法
//                              request.getRequestDispatcher("success.jsp")
//                                      .forward(request, response);
                                // 註冊成功, Redirect法
                                response.sendRedirect("success.jsp");
                                
                        }else{
                                // 註冊失敗
                                response.sendRedirect("reg2.jsp");
                                
                        }
                        
                }*/
                
        }

        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //doGet(request, response);
                String userid = null;
                String name = null;
                String nickname = null;
                String pw1 = null;
                String pw2 = null;
                String PID = null;
                String sex = null;
                String phone = null;
                String logintype = null;
                String email = null;
                
                request.setCharacterEncoding("UTF-8");
                response.setCharacterEncoding("UTF-8");                
                
                if (request.getParameter("userid")!=null){
                        userid = request.getParameter("userid");
                        nickname = request.getParameter("nickname");
                        pw1 = request.getParameter("pw1");
                        pw2 = request.getParameter("pw2");
                        PID = request.getParameter("PID");
                        sex = request.getParameter("sex");
                        phone = request.getParameter("phone");
                        email = request.getParameter("email");
                        logintype = request.getParameter("logintype");
                                                                      
                        Member m = new Member(userid, name, nickname, pw1, pw2, PID, sex, phone, email, logintype);
                        HttpSession session = request.getSession();
                        session.setAttribute("m", m);
                        
                        if (m.validate()){
                                // 註冊成功 , Forward法
//                              request.getRequestDispatcher("success.jsp")
//                                      .forward(request, response);
                                // 註冊成功, Redirect法
                                response.sendRedirect("success.jsp");
                                
                        }else{
                                // 註冊失敗
                                response.sendRedirect("reg2.jsp");
                                
                        }
                        
                }
        }

}
